package com.huishu.tcm.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.huishu.tcm.vo.SmsResponseVo;

import java.util.HashMap;
import java.util.Map;

public class SmsUtil {

    /*
    id: LTAI5tRS27AfgbaLg3i2Aqg7
    Secret: jwycHHivjoQ2tMatNGibnVrmuIyvQM
     */
    public static Client smsClient;

    public static Client createClient() throws Exception {
        if(ObjectUtil.isNotEmpty(smsClient)){
            return smsClient;
        }
        Config config = new Config()
            .setAccessKeyId("LTAI5tRS27AfgbaLg3i2Aqg7")
            .setAccessKeySecret("jwycHHivjoQ2tMatNGibnVrmuIyvQM");
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

    // 发送短信
    public static SmsResponseVo sendSms(String telNum,String verificationCode) throws Exception {
        Map<String,String> map = new HashMap<>();
        map.put("code",verificationCode);
        smsClient = createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
            .setPhoneNumbers(telNum)
            .setSignName("创研糖管家")
            .setTemplateCode("SMS_474005019")
            .setTemplateParam(JSONObject.toJSONString(map));
        SendSmsResponse sendSmsResponse = smsClient.sendSms(sendSmsRequest);
        SmsResponseVo smsResponseVo = new SmsResponseVo();
        smsResponseVo.setStatusCode(sendSmsResponse.getStatusCode());
        smsResponseVo.setCode(sendSmsResponse.getBody().getCode());
        smsResponseVo.setBizId(sendSmsResponse.getBody().getBizId());
        smsResponseVo.setMessage(sendSmsResponse.getBody().getMessage());
        smsResponseVo.setRequestId(sendSmsResponse.getBody().getRequestId());
        return smsResponseVo;
    }

    // 医生注册 审核是否通过
    public static SmsResponseVo sendDoctorRegisterSuccessSms(String telNum, boolean isSuccess) throws Exception {
        String templateCode = "SMS_473985024";
        if(isSuccess){
            templateCode = "SMS_473820134";
        }
        smsClient = createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
            .setPhoneNumbers(telNum)
            .setSignName("创研糖管家")
            .setTemplateCode(templateCode);
        SendSmsResponse sendSmsResponse = smsClient.sendSms(sendSmsRequest);
        SmsResponseVo smsResponseVo = new SmsResponseVo();
        smsResponseVo.setStatusCode(sendSmsResponse.getStatusCode());
        smsResponseVo.setCode(sendSmsResponse.getBody().getCode());
        smsResponseVo.setBizId(sendSmsResponse.getBody().getBizId());
        smsResponseVo.setMessage(sendSmsResponse.getBody().getMessage());
        smsResponseVo.setRequestId(sendSmsResponse.getBody().getRequestId());
        return smsResponseVo;
    }

    public static void main(String[] args) throws Exception {
        SmsResponseVo smsResponseVo = SmsUtil.sendSms("13045067128","1234");
        System.out.println(JSONObject.toJSONString(smsResponseVo));
    }

}
